<?php
/**
 * 创建缺失的必要表
 * 直接数据库连接，不依赖框架
 */

// 数据库配置
$host = '101.35.252.112';
$port = '45508';
$dbname = 'trace';
$username = 'root';
$password = 'CeyZFHxjPABhp2Hz';

try {
    // 创建数据库连接
    $pdo = new PDO("mysql:host=$host;port=$port;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "✅ 数据库连接成功\n";
    
    // SQL语句数组
    $sqlStatements = [
        // 数据导出记录表
        "CREATE TABLE IF NOT EXISTS `gf_data_exports` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `user_id` int(11) NOT NULL,
          `app_id` int(11) NOT NULL,
          `export_type` varchar(50) NOT NULL,
          `file_path` varchar(500) NOT NULL,
          `file_size` varchar(50) DEFAULT NULL,
          `record_count` int(11) DEFAULT '0',
          `date_range_start` date DEFAULT NULL,
          `date_range_end` date DEFAULT NULL,
          `format` varchar(20) NOT NULL,
          `advanced_options` text,
          `status` varchar(20) DEFAULT 'completed',
          `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
          PRIMARY KEY (`id`),
          KEY `idx_user_id` (`user_id`),
          KEY `idx_app_id` (`app_id`),
          KEY `idx_created_at` (`created_at`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
        
        // 反欺诈高风险记录表
        "CREATE TABLE IF NOT EXISTS `gf_antifraud_high_risk` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `device_fingerprint` varchar(64) NOT NULL,
          `ip_address` varchar(45) DEFAULT NULL,
          `risk_score` int(11) DEFAULT '0',
          `risk_reason` text,
          `appkey` varchar(100) DEFAULT NULL,
          `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
          PRIMARY KEY (`id`),
          KEY `idx_device_fingerprint` (`device_fingerprint`),
          KEY `idx_ip_address` (`ip_address`),
          KEY `idx_created_at` (`created_at`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
        
        // 归因追踪表
        "CREATE TABLE IF NOT EXISTS `gf_attribution_track` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `appkey` varchar(100) NOT NULL,
          `device_fingerprint` varchar(64) NOT NULL,
          `ip_address` varchar(45) DEFAULT NULL,
          `user_agent` text,
          `referer` text,
          `utm_source` varchar(255) DEFAULT NULL,
          `utm_medium` varchar(255) DEFAULT NULL,
          `utm_campaign` varchar(255) DEFAULT NULL,
          `click_id` varchar(255) DEFAULT NULL,
          `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
          PRIMARY KEY (`id`),
          KEY `idx_appkey` (`appkey`),
          KEY `idx_device_fingerprint` (`device_fingerprint`),
          KEY `idx_created_at` (`created_at`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
        
        // 归因安装表
        "CREATE TABLE IF NOT EXISTS `gf_attribution_install` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `appkey` varchar(100) NOT NULL,
          `device_fingerprint` varchar(64) NOT NULL,
          `ip_address` varchar(45) DEFAULT NULL,
          `install_time` datetime DEFAULT CURRENT_TIMESTAMP,
          `attribution_source` varchar(100) DEFAULT NULL,
          `campaign_name` varchar(255) DEFAULT NULL,
          `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
          PRIMARY KEY (`id`),
          KEY `idx_appkey` (`appkey`),
          KEY `idx_device_fingerprint` (`device_fingerprint`),
          KEY `idx_install_time` (`install_time`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
        
        // 归因事件表
        "CREATE TABLE IF NOT EXISTS `gf_attribution_event` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `appkey` varchar(100) NOT NULL,
          `device_fingerprint` varchar(64) NOT NULL,
          `event_type` varchar(100) NOT NULL,
          `event_value` text,
          `ip_address` varchar(45) DEFAULT NULL,
          `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
          PRIMARY KEY (`id`),
          KEY `idx_appkey` (`appkey`),
          KEY `idx_device_fingerprint` (`device_fingerprint`),
          KEY `idx_event_type` (`event_type`),
          KEY `idx_created_at` (`created_at`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"
    ];
    
    // 执行每个SQL语句
    foreach ($sqlStatements as $index => $sql) {
        try {
            $pdo->exec($sql);
            $tableName = '';
            if (preg_match('/CREATE TABLE IF NOT EXISTS `([^`]+)`/', $sql, $matches)) {
                $tableName = $matches[1];
            }
            echo "✅ 创建表成功: $tableName\n";
        } catch (PDOException $e) {
            echo "❌ 创建表失败 (语句 " . ($index + 1) . "): " . $e->getMessage() . "\n";
        }
    }
    
    // 验证表是否创建成功
    $tablesToCheck = ['gf_data_exports', 'gf_antifraud_high_risk', 'gf_attribution_track', 'gf_attribution_install', 'gf_attribution_event'];
    
    foreach ($tablesToCheck as $table) {
        $stmt = $pdo->query("SHOW TABLES LIKE '$table'");
        if ($stmt->rowCount() > 0) {
            echo "✅ 验证成功: $table 表已存在\n";
        } else {
            echo "⚠️  验证失败: $table 表不存在\n";
        }
    }
    
    echo "\n✅ 所有缺失表创建完成\n";
    
} catch (PDOException $e) {
    echo "❌ 数据库连接失败: " . $e->getMessage() . "\n";
    exit(1);
}